package com.bxd.baodan.activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.util.ShareUtil;
import com.bxd.baodan.widget.IosDialog;
import com.umeng.analytics.MobclickAgent;

public class StartDesignActivity extends BaseActivity implements View.OnClickListener {


    private LinearLayout personalDesign;
    private LinearLayout familyDesign;
    private TextView privacyStatement;
    private ImageView share;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_design);
        setActionBarTitle("智能保险方案设计");
        share = (ImageView) findViewById(R.id.iv_bar_right);
        share.setImageResource(R.drawable.share);
        share.setVisibility(View.VISIBLE);
        personalDesign = (LinearLayout) findViewById(R.id.personal_design);
        familyDesign = (LinearLayout) findViewById(R.id.family_design);
        privacyStatement = (TextView) findViewById(R.id.privacy_statement);

        createHandler();

        personalDesign.setOnClickListener(this);
        share.setOnClickListener(this);
        familyDesign.setOnClickListener(this);
        privacyStatement.setOnClickListener(this);


        WindowManager wm = (WindowManager) this
                .getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();

        float we = width * 1f - getResources().getDimension(R.dimen.design_margin);


        /*
        LinearInterpolator(匀速）
AccelerateInterpolator（先慢后快）
AccelerateDecelerateInterpolator（先慢中快后慢）
DecelerateInterpolator（先快后慢）
CycleInterpolator（循环播放，速度为正弦曲线）
AnticipateInterpolator（先回撤，再匀速向前）
OvershootInterpolator（超过，拉回）
BounceInterpolator(回弹）
*/

        ObjectAnimator transAnim = ObjectAnimator.ofFloat(personalDesign, "translationX", -we / 2, 0);
        ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(personalDesign, "alpha", 0, 1);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(transAnim).with(alphaAnim);
        animatorSet.setDuration(800);
        animatorSet.setInterpolator(new BounceInterpolator());
        animatorSet.start();

        ObjectAnimator transAnim1 = ObjectAnimator.ofFloat(familyDesign, "translationX", -we, 0);
        ObjectAnimator alphaAnim1 = ObjectAnimator.ofFloat(familyDesign, "alpha", 0, 1);
        AnimatorSet animatorSet1 = new AnimatorSet();
        animatorSet1.play(transAnim1).with(alphaAnim1);
        animatorSet1.setDuration(800);
        animatorSet1.setStartDelay(200);
        animatorSet1.setInterpolator(new BounceInterpolator());
        animatorSet1.start();

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.personal_design:

                if (loginStatus()) {
                    MobclickAgent.onEvent(App.getContext(), "v6_0_0_SY_znfasj_gr");
                    openActivity(PersonalDesignActivity.class, new String[]{"type"}, new String[]{"1"});
                }
                break;


            case R.id.family_design:
                if (loginStatus()) {
                    MobclickAgent.onEvent(App.getContext(), "v6_0_0_SY_znfasj_jt");
                    openActivity(FamilyDesignActivity.class);
                }


                break;
            case R.id.iv_bar_right:
                String url = "http://rest-02.bxd365.com/html/design/personal?step=1";
                String imgUrl = "";
                ShareUtil.showShareWindow(StartDesignActivity.this,
                        "智能保险方案设计",
                        "人工智能为您精准测算保险需求,快来试试吧!",
                        url,
                        imgUrl,
                        "",
                        handler);
                break;

            case R.id.privacy_statement:
                // TODO: 17/8/29  可能是弹出窗体
                new IosDialog(StartDesignActivity.this).builder()
                        .setTitle("隐私声明")
                        .setMsg("该评测工具由保险岛为广大用户打造,我们承诺您提供的个人信息仅用于分析您的保险需求,绝不泄漏。")
                        .setNegativeButton("确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        }).show();

                break;

        }

    }

    private void createHandler() {
        if (handler == null) {

            handler = new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(Message msg) {

                    switch (msg.what) {

                        case ShareUtil.SHARE_ERROR:
                            Toast.makeText(StartDesignActivity.this, "分享失败",
                                    Toast.LENGTH_SHORT).show();
                            break;

                        case ShareUtil.SHARE_CANCELED:
                            Toast.makeText(StartDesignActivity.this, "取消分享",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        default:
                            break;
                    }
                    return false;
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        handler.removeCallbacksAndMessages(null);
        super.onDestroy();

    }
}
